package com.ssy.lingxi.logistics.serviceimpl;

import com.ssy.lingxi.logistics.entity.Company;
import com.ssy.lingxi.logistics.entity.LogisticsOrder;
import com.ssy.lingxi.logistics.repository.CompanyRepository;
import com.ssy.lingxi.logistics.service.ILogisticsOrderReportService;
import com.ssy.lingxi.report.api.enums.LogisticsOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.OperateDataSourceEnum;
import com.ssy.lingxi.report.api.feign.ReportControllerFeign;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 物流单报表服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2021/1/26
 */
@Slf4j
@Service
public class LogisticsOrderReportServiceImpl implements ILogisticsOrderReportService {

    @Resource
    private ReportControllerFeign reportControllerFeign;
    @Resource
    private CompanyRepository companyRepository;

    @Override
    public void createOperateNotice(LogisticsOrder logisticsOrder) {
        // 物流单待提交
        OperateNoticeVO operateNoticeVO = new OperateNoticeVO();
        operateNoticeVO.setMemberId(logisticsOrder.getMemberId());
        operateNoticeVO.setRoleId(logisticsOrder.getRoleId());
        operateNoticeVO.setDataSource(OperateDataSourceEnum.LOGISTICS.getCode());
        operateNoticeVO.setIncreaseOperateType(LogisticsOperateTypeEnum.TO_BE_COMMIT.getCode());
        operateNoticeVO.setIncreaseCount(1L);
        try {
            reportControllerFeign.addOperateNotice(operateNoticeVO);
        } catch (Exception e) {
            log.error("物流单新增 - 批量新增操作通知", e);
        }
    }

    @Override
    public void deleteOperateNotice(LogisticsOrder logisticsOrder) {
        // 物流单待提交
        OperateNoticeVO operateNoticeVO = new OperateNoticeVO();
        operateNoticeVO.setMemberId(logisticsOrder.getMemberId());
        operateNoticeVO.setRoleId(logisticsOrder.getRoleId());
        operateNoticeVO.setDataSource(OperateDataSourceEnum.LOGISTICS.getCode());
        operateNoticeVO.setReduceOperateType(LogisticsOperateTypeEnum.TO_BE_COMMIT.getCode());
        operateNoticeVO.setReduceCount(1L);
        try {
            reportControllerFeign.addOperateNotice(operateNoticeVO);
        } catch (Exception e) {
            log.error("物流单删除 - 批量新增操作通知", e);
        }
    }

    @Override
    public void submitNormalOperateNotice(LogisticsOrder logisticsOrder) {
        // 物流服务商信息
        Company company = companyRepository.findById(logisticsOrder.getCompanyId()).orElse(null);
        if (Objects.isNull(company)) {
            return;
        }

        // 物流单待提交
        OperateNoticeVO waitSubmitOperateNotice = new OperateNoticeVO();
        waitSubmitOperateNotice.setMemberId(logisticsOrder.getMemberId());
        waitSubmitOperateNotice.setRoleId(logisticsOrder.getRoleId());
        waitSubmitOperateNotice.setDataSource(OperateDataSourceEnum.LOGISTICS.getCode());
        waitSubmitOperateNotice.setReduceOperateType(LogisticsOperateTypeEnum.TO_BE_COMMIT.getCode());
        waitSubmitOperateNotice.setReduceCount(1L);

        // 物流单待确认
        OperateNoticeVO waitConfirmOperateNotice = new OperateNoticeVO();
        waitConfirmOperateNotice.setMemberId(company.getCompanyMemberId());
        waitConfirmOperateNotice.setRoleId(company.getCompanyRoleId());
        waitConfirmOperateNotice.setDataSource(OperateDataSourceEnum.LOGISTICS.getCode());
        waitConfirmOperateNotice.setIncreaseOperateType(LogisticsOperateTypeEnum.TO_BE_CONFIRM.getCode());
        waitConfirmOperateNotice.setIncreaseCount(1L);

        List<OperateNoticeVO> operateNoticeVOList = new ArrayList<>();
        operateNoticeVOList.add(waitSubmitOperateNotice);
        operateNoticeVOList.add(waitConfirmOperateNotice);

        try {
            reportControllerFeign.addOperateNoticeList(operateNoticeVOList);
        } catch (Exception e) {
            log.error("物流单提交 - 批量新增操作通知", e);
        }
    }

    @Override
    public void submitOtherOperateNotice(LogisticsOrder logisticsOrder) {
        // 物流服务商信息
        Company company = companyRepository.findById(logisticsOrder.getCompanyId()).orElse(null);
        if (Objects.isNull(company)) {
            return;
        }

        // 物流单待确认
        OperateNoticeVO waitConfirmOperateNotice = new OperateNoticeVO();
        waitConfirmOperateNotice.setMemberId(company.getCompanyMemberId());
        waitConfirmOperateNotice.setRoleId(company.getCompanyRoleId());
        waitConfirmOperateNotice.setDataSource(OperateDataSourceEnum.LOGISTICS.getCode());
        waitConfirmOperateNotice.setIncreaseOperateType(LogisticsOperateTypeEnum.TO_BE_CONFIRM.getCode());
        waitConfirmOperateNotice.setIncreaseCount(1L);

        try {
            reportControllerFeign.addOperateNotice(waitConfirmOperateNotice);
        } catch (Exception e) {
            log.error("物流单提交 - 批量新增操作通知", e);
        }
    }

    @Override
    public void confirmOperateNotice(LogisticsOrder logisticsOrder) {
        // 物流服务商信息
        Company company = companyRepository.findById(logisticsOrder.getCompanyId()).orElse(null);
        if (Objects.isNull(company)) {
            return;
        }

        // 物流单确认
        OperateNoticeVO waitConfirmOperateNotice = new OperateNoticeVO();
        waitConfirmOperateNotice.setMemberId(company.getCompanyMemberId());
        waitConfirmOperateNotice.setRoleId(company.getCompanyRoleId());
        waitConfirmOperateNotice.setDataSource(OperateDataSourceEnum.LOGISTICS.getCode());
        waitConfirmOperateNotice.setReduceOperateType(LogisticsOperateTypeEnum.TO_BE_CONFIRM.getCode());
        waitConfirmOperateNotice.setReduceCount(1L);
        try {
            reportControllerFeign.addOperateNotice(waitConfirmOperateNotice);
        } catch (Exception e) {
            log.error("物流单确认 - 批量新增操作通知", e);
        }
    }
}